/**   
* @Title: MainProjectCreateLogic.java 
* @Package cn.com.gome.generator.logic 
* @Description: 主项目创建
* @author chenmin-ds   
* @date 2017年5月26日 下午2:49:05 
* @company cn.com.gome
* @version V1.0   
*/ 


package cn.com.gome.generator.logic;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.com.gome.framework.dao.entity.TblChildProjectInfo;
import cn.com.gome.framework.dao.entity.TblProjectBasicInfo;

import com.gomeplus.frame.exception.LogicsException;
import com.gomeplus.frame.logic.ILogics;
import com.gomeplus.frame.logic.ResultEnum;

/** 
 * @ClassName: MainProjectCreateLogic 
 * @Description: 主项目创建
 * @author chenmin-ds 
 * @date 2017年5月26日 下午2:49:05  
 */
@Service
public class ChildProjectTaskCreateLogic implements ILogics<Map<String , Object>> {

	private Logger logger = LoggerFactory.getLogger("ChildProjectAdminCreateLogic");
	
	public ResultEnum exec(Map<String , Object> map) throws LogicsException {
		try{
			TblChildProjectInfo childProjectInfo = (TblChildProjectInfo) map.get("childProjectInfo");
			TblProjectBasicInfo projectBasicInfo = (TblProjectBasicInfo) map.get("projectBasicInfo");
			String pachagesPath = projectBasicInfo.getPackages().replace(".", "//");
			
			String projectPath = projectBasicInfo.getProjectPath().trim();
			if(!projectPath.endsWith("//")){
				projectPath +="//";
				projectBasicInfo.setProjectPath(projectPath);
			}
			
			projectPath += projectBasicInfo.getProjectEng()+"//";
			projectPath += projectBasicInfo.getProjectEng()+"-"+childProjectInfo.getChildProjectEng();
			childProjectInfo.setProjectPath(projectPath);
			File file = new File(projectPath+"//src//main//resources//spring.xml");
			if (!file.exists()) {
				file.createNewFile();
			}
			StringBuffer sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n");
			sb.append("<beans xmlns=\"http://www.springframework.org/schema/beans\" \n");
			sb.append("			xmlns:util=\"http://www.springframework.org/schema/util\" \n");
			sb.append("			xmlns:aop=\"http://www.springframework.org/schema/aop\" \n");
			sb.append("			xmlns:context=\"http://www.springframework.org/schema/context\" \n");
			sb.append("			xmlns:tx=\"http://www.springframework.org/schema/tx\" \n");
			sb.append("			xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n");
			sb.append("			xmlns:mongo=\"http://www.springframework.org/schema/data/mongo\" \n");
			sb.append("			xmlns:task=\"http://www.springframework.org/schema/task\" \n");
			sb.append("			xsi:schemaLocation=\"http://www.springframework.org/schema/aop \n");
			sb.append("								http://www.springframework.org/schema/aop/spring-aop.xsd \n");
			sb.append("								http://www.springframework.org/schema/beans \n");
			sb.append("								http://www.springframework.org/schema/beans/spring-beans.xsd\n"); 
			sb.append("								http://www.springframework.org/schema/context \n");
			sb.append("								http://www.springframework.org/schema/context/spring-context.xsd \n");
			sb.append("								http://www.springframework.org/schema/tx \n");
			sb.append("								http://www.springframework.org/schema/tx/spring-tx.xsd \n");
			sb.append("								http://www.springframework.org/schema/task \n");
			sb.append("								http://www.springframework.org/schema/task/spring-task-3.1.xsd \n");
			sb.append("								http://www.springframework.org/schema/util \n");
			sb.append("								http://www.springframework.org/schema/util/spring-util-3.1.xsd\">\n");
			sb.append("  \n");
			sb.append("  <aop:aspectj-autoproxy proxy-target-class=\"true\" /> \n");
			sb.append("  <context:annotation-config /> \n");
			if("com.gomeplus".equals(projectBasicInfo.getPackages())){
				sb.append("  <context:component-scan base-package=\"com.gomeplus\" /> \n");
			}else{
				sb.append("  <context:component-scan base-package=\""+projectBasicInfo.getPackages()+",com.gomeplus\" /> \n");
			}
			sb.append("  \n");
			sb.append("  <bean id=\"springBeanFactory\" class=\"com.gomeplus.frame.factory.SpringBeanFactory\" /> \n");
			sb.append("  \n");
			sb.append("  <import resource=\"classpath*:/spring/"+projectBasicInfo.getProjectEng()+"-jdbc-props.xml\" /> \n");
			sb.append("  <import resource=\"classpath*:/spring/"+projectBasicInfo.getProjectEng()+"-dataSource.xml\" /> \n");
			sb.append("  \n");
			sb.append("  <import resource=\"classpath*:/dubbo/dubbo-head.xml\" /> \n");
			sb.append("  <import resource=\"spring/redis-dubbo-consumer.xml\" /> \n");
			sb.append("  <import resource=\"spring/spring-quertz.xml\" /> \n");
			sb.append("</beans>\n");
			OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(projectPath+"//src//main//resources//spring.xml"), "UTF-8");
			BufferedWriter writer = new BufferedWriter(write);
			writer.write(sb.toString());
			writer.flush();
			writer.close();
			
			file = new File(projectPath+"//src//main//resources//log4j.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			sb = new StringBuffer();
			sb.append("log4j.rootLogger=DEBUG,stdout,fileAppender\n");                                                         
			sb.append("log4j.logger."+projectBasicInfo.getPackages()+"."+projectBasicInfo.getProjectEng()+".dao.mapper=DEBUG,ibatis \n");                                                
			sb.append("\n");                                                                                                  
			sb.append("log4j.appender.stdout=org.apache.log4j.ConsoleAppender\n");                                             
			sb.append("log4j.appender.stdout.layout=org.apache.log4j.PatternLayout\n");                                        
			sb.append("log4j.appender.stdout.DatePattern='_'yyyy-MM-dd'.log'\n");                                              
			sb.append("log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\\:mm\\:ss} %p [%c] %m%n\n");              
			sb.append("\n");                                                                                             
			sb.append("log4j.appender.fileAppender=org.apache.log4j.DailyRollingFileAppender\n");                              
			sb.append("log4j.appender.fileAppender.File=../logs/"+projectBasicInfo.getProjectEng()+"_task_info.log\n");                                 
			sb.append("log4j.appender.fileAppender.DatePattern='_'yyyy-MM-dd'.log'\n");                                        
			sb.append("log4j.appender.fileAppender.Threshold=INFO\n");                                                         
			sb.append("log4j.appender.fileAppender.Encoding=UTF-8\n");                                                         
			sb.append("log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout\n");                                  
			sb.append("log4j.appender.fileAppender.layout.ConversionPattern=%-d{yyyyMMdd HH:mm:ss SSS}-->[%-5p]--[%c{1}]--%m%\n"); 
			sb.append("\n");                                                                                                   
			sb.append("log4j.appender.ibatis=org.apache.log4j.DailyRollingFileAppender\n");                                    
			sb.append("log4j.appender.ibatis.File=../logs/"+projectBasicInfo.getProjectEng()+"_task_ibatis.log\n");                                     
			sb.append("log4j.appender.ibatis.DatePattern='_'yyyy-MM-dd'.log'\n");                                              
			sb.append("log4j.appener.ibatis.Threshold=DEBUG\n");                                                               
			sb.append("log4j.appender.ibatis.Encoding=UTF-8\n");                                                               
			sb.append("log4j.appender.ibatis.layout=org.apache.log4j.PatternLayout\n");                                        
			sb.append("log4j.appender.ibatis.layout.ConversionPattern=%-d{yyyyMMdd HH:mm:ss SSS}-->[%-5p]--[%c{1}]--%m%nd\n"); 
			write = new OutputStreamWriter(new FileOutputStream(projectPath+"//src//main//resources//log4j.properties"), "UTF-8");
			writer = new BufferedWriter(write);
			writer.write(sb.toString());
			writer.flush();
			writer.close();
			
			file = new File(projectPath+"//src//main//resources//spring");
			if (!file.exists()) {
				file.mkdirs();
			}
			
			file = new File(projectPath+"//src//main//resources//spring//redis-dubbo-consumer.xml");
			if (!file.exists()) {
				file.createNewFile();
			}
			sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			sb.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"\n");
			sb.append("	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dubbo=\"http://code.alibabatech.com/schema/dubbo\"\n");
			sb.append("	xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\n");
			sb.append("	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd\">\n");
			sb.append("	<bean id=\"jjcsGcache\" class=\"redis.GcacheClient\" destroy-method=\"close\">\n");
			sb.append("	<!-- zookeeperAddress -->\n");
			sb.append("	<constructor-arg value=\"${dubbo.run.redis}\" />\n");
			sb.append("	<!-- businessName -->\n");
			sb.append("	<constructor-arg value=\"${redis.businessName}\" />\n");
			sb.append("	</bean>\n");
			sb.append("</beans>\n");
			write = new OutputStreamWriter(new FileOutputStream(projectPath+"//src//main//resources//spring//redis-dubbo-consumer.xml"), "UTF-8");
			writer = new BufferedWriter(write);
			writer.write(sb.toString());
			writer.flush();
			writer.close();
			
			file = new File(projectPath+"//src//main//resources//spring//spring-quertz.xml");
			if (!file.exists()) {
				file.createNewFile();
			}
			sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			sb.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"\n");
			sb.append("	xmlns:util=\"http://www.springframework.org/schema/util\" xmlns:aop=\"http://www.springframework.org/schema/aop\"\n");
			sb.append("	xmlns:context=\"http://www.springframework.org/schema/context\" xmlns:tx=\"http://www.springframework.org/schema/tx\"\n");
			sb.append("	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:mongo=\"http://www.springframework.org/schema/data/mongo\"\n");
			sb.append("	xmlns:task=\"http://www.springframework.org/schema/task\" xmlns:mvc=\"http://www.springframework.org/schema/mvc\"\n");
			sb.append("	xsi:schemaLocation=\"http://www.springframework.org/schema/aop  \n");
			sb.append("        http://www.springframework.org/schema/aop/spring-aop.xsd  \n");
			sb.append("        http://www.springframework.org/schema/beans  \n");
			sb.append("        http://www.springframework.org/schema/beans/spring-beans.xsd  \n");
			sb.append("        http://www.springframework.org/schema/context  \n");
			sb.append("        http://www.springframework.org/schema/context/spring-context.xsd  \n");
			sb.append("        http://www.springframework.org/schema/tx  \n");
			sb.append("        http://www.springframework.org/schema/tx/spring-tx.xsd\n");
			sb.append("        http://www.springframework.org/schema/task  \n");
			sb.append("		http://www.springframework.org/schema/task/spring-task-3.1.xsd\n");
			sb.append("		http://www.springframework.org/schema/util \n");
			sb.append("		http://www.springframework.org/schema/util/spring-util-3.1.xsd\n");
			sb.append("		http://www.springframework.org/schema/mvc\n");
			sb.append("		http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd\n");
			sb.append("		\">\n");
			sb.append("\n");
			sb.append("	<bean name=\"schedulerFactoryBean\" class=\"org.springframework.scheduling.quartz.SchedulerFactoryBean\">         \n");
			sb.append("		<property name=\"applicationContextSchedulerContextKey\">\n");
			sb.append("			<value>applicationContext</value>\n");
			sb.append("		</property>\n");
			sb.append("	</bean>       \n");
			sb.append("	\n");
			sb.append("</beans>\n");
			write = new OutputStreamWriter(new FileOutputStream(projectPath+"//src//main//resources//spring//spring-quertz.xml"), "UTF-8");
			writer = new BufferedWriter(write);
			writer.write(sb.toString());
			writer.flush();
			writer.close();
			
			file = new File(projectPath+"//src//main//resources//props//dev");
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(projectPath+"//src//main//resources//props//dev//"+projectBasicInfo.getProjectEng()+"-task.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			file = new File(projectPath+"//src//main//resources//props//uat");
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(projectPath+"//src//main//resources//props//uat//"+projectBasicInfo.getProjectEng()+"-task.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			file = new File(projectPath+"//src//main//resources//props//live");
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(projectPath+"//src//main//resources//props//live//"+projectBasicInfo.getProjectEng()+"-task.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			file = new File(projectPath+"//src//main//resources//props//pre");
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(projectPath+"//src//main//resources//props//pre//"+projectBasicInfo.getProjectEng()+"-task.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			
			file = new File(projectPath+"//src//main//resources//props//task.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			file = new File(projectPath+"//src//main//java//"+pachagesPath+"//task");
			if (!file.exists()) {
				file.mkdirs();
			}
			
			file = new File(projectPath+"//src//main//java//"+pachagesPath+"//"+projectBasicInfo.getProjectEng()+"//logic//impl");
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(projectPath+"//src//main//java//"+pachagesPath+"//"+projectBasicInfo.getProjectEng()+"//step//impl");
			if (!file.exists()) {
				file.mkdirs();
			}
			
			
			file = new File(projectPath+"//src//main//webapp//WEB-INF");
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(projectPath+"//src//main//webapp//WEB-INF//web.xml");
			if (!file.exists()) {
				file.createNewFile();
			}
			sb = new StringBuffer();
			sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			sb.append("<web-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n");     
			sb.append("	xmlns=\"http://java.sun.com/xml/ns/javaee\" \n");     
			sb.append("	xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" \n");     
			sb.append("	version=\"2.5\" metadata-complete=\"true\">\n");     
			sb.append("  <context-param>\n");     
			sb.append("  	<param-name>contextConfigLocation</param-name> \n");     
			sb.append("  	<param-value>classpath:spring.xml</param-value> \n");     
			sb.append("  </context-param>\n");     
			sb.append("  <context-param>\n");     
			sb.append("  	<param-name>propertiesConfigLocation</param-name> \n");     
			sb.append("  	<param-value>/props/task.properties</param-value>\n");      
			sb.append("  </context-param>\n");     
			sb.append("  <listener>\n");     
			sb.append("  	<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> \n");     
			sb.append("  </listener>\n");     
			sb.append("  <listener>\n");     
			sb.append("  	<listener-class>com.gomeplus.frame.listener.PropertiesConfigListener</listener-class> \n");     
			sb.append("  </listener>\n");     
			sb.append("  <listener>\n");     
			sb.append("  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> \n");     
			sb.append("  </listener>\n");     
			sb.append("</web-app>\n");  
			write = new OutputStreamWriter(new FileOutputStream(projectPath+"//src//main//webapp//WEB-INF//web.xml"), "UTF-8");
			writer = new BufferedWriter(write);
			writer.write(sb.toString());
			writer.flush();
			writer.close();
			
			
			return ResultEnum.OK;
		}catch(Exception e){
			logger.error("创建子项目异常：",e);
		}
		return ResultEnum.PART_CASE_01;
	}

}
